package com.emm.yixun.website.listeners;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;

import com.emm.yixun.common.enums.ResponseEnums;
import com.emm.yixun.common.model.ActcommitLog;
import com.emm.yixun.common.model.PriceScheme;
import com.emm.yixun.common.model.dto.ActcommitLogDto;
import com.emm.yixun.common.model.dto.NoticeEntity;
import com.emm.yixun.common.model.dto.PriceSchemeDto;
import com.emm.yixun.common.model.enums.PriceSchemeStatusEnum;
import com.emm.yixun.common.response.Response;
import com.emm.yixun.common.response.ResponseList;
import com.emm.yixun.marketing.service.PriceSchemeServiceFacade;
import com.emm.yixun.system.service.notice.pricescheme.PriceSchemeValidNoticeFacade;
import com.emm.yixun.website.common.utils.SpringWebContextUtil;
import com.emm.yixun.website.service.ActivitiProcessServiceImpl;
import com.emm.yixun.website.service.system.log.ActCommitLogService;

/**
* 类描述： 流程结果事件监听
* 创建者： ThinkPad
* 项目名称： emm-website
* 创建时间： 2016年6月27日 下午2:17:36
* 版本号： v1.0
 */
public class PriceSchemeListeners implements Serializable, ExecutionListener{

	private static final long serialVersionUID = -8417744322348643044L;

	@Override
	public void notify(DelegateExecution arg0) throws Exception {
		//事件结束后 更改业务ID数据为已审批
		PriceSchemeServiceFacade priceSchemeServiceFacade=getPriceSchemeServiceFacade();
		String id=arg0.getProcessBusinessKey();
		Response<PriceScheme> response = priceSchemeServiceFacade.findById(new Long(id));
		PriceSchemeDto dto = new PriceSchemeDto();
		dto.setId(new Long(id));
		if(response.isSuccess()){
			//审核通过
			PriceScheme entity = response.getEntity();
			//判断审核状态
			if(getActivitiProcessServiceImpl().isCheckPass(arg0.getProcessInstanceId())){
				boolean flag = checkAuditCommit(entity);
				if(!flag) {
					throw new Exception(ResponseEnums.RESPONSEENUMS_999999.getName());
				}
				
				if(entity.getExecutionDate()==null){
					dto.setExecutionDate(new Date());
					//没有设置方案执行时间的理解生效
					dto.setStatus("10");
					priceSchemeServiceFacade.taskRun(new Long(id));
				}else{
					if(entity.getExecutionDate().getTime()<new Date().getTime()){
						dto.setStatus("10");
						priceSchemeServiceFacade.taskRun(new Long(id));
					}else{
						//设置了方案执行时间的,方案的状态为审核通过
						dto.setStatus("01");
					}
				}
				priceSchemeServiceFacade.updateByPrimaryKey(dto, true);
				/**
				 * （价格方案流程审核通过）推送消息到app
				 * @author ny
				 * @param param noticeType 提醒类型, noticeBusinessId 业务主键Id
				 * @return 价格方案审核结果【${auditStatus}】方案名称：${schemeName}
				 */
				NoticeEntity noticeEntity = new NoticeEntity();
				noticeEntity.setNoticeBusinessId(Long.valueOf(id));
				noticeEntity.setNoticeType("14");
				noticeEntity.setProcessInstanceId(arg0.getProcessInstanceId());
				getPriceSchemeValidNoticeFacade().createPriceSchemeValidNotice(noticeEntity);
			}else{
				
				boolean flag = checkAuditCommit(entity);
				if(!flag) {
					throw new Exception(ResponseEnums.RESPONSEENUMS_999999.getName());
				}
				
				//审核不通过
				dto.setStatus("02");
				priceSchemeServiceFacade.updateByPrimaryKey(dto, true);
				/**
				 * （价格方案流程审核不通过）推送消息到app
				 * @author ny
				 * @param param noticeType 提醒类型, noticeBusinessId 业务主键Id
				 * @return 价格方案审核结果【${auditStatus}】方案名称：${schemeName}
				 */
				NoticeEntity noticeEntity = new NoticeEntity();
				noticeEntity.setNoticeBusinessId(Long.valueOf(id));
				noticeEntity.setNoticeType("14");
				noticeEntity.setProcessInstanceId(arg0.getProcessInstanceId());
				getPriceSchemeValidNoticeFacade().createPriceSchemeValidNotice(noticeEntity);
			}
		
		}
	}
	
	/**
	 * 前置判断,审核记录是否已经提交,防止重复提交
	 * 产生原因: 提交审核,后台处理中时,刷新页面再次提交审核,会重复
	 * true: 可继续执行
	 * false: 不可执行
	 */
	public boolean checkAuditCommit(PriceScheme entity) {
		
		// 业务数据为null时,默认通过.为了不改变老业务,不影响流程正常行走
		if (null == entity) {
			return true;
		}
		
		//1、获取提交状态
		//价格方案状态特殊,单独处理
		String status = entity.getStatus();
		//已审核完成的记录,直接返回
		if(PriceSchemeStatusEnum.REJECT.getNo().equals(status)
			|| PriceSchemeStatusEnum.VALID.getNo().equals(status)
			|| PriceSchemeStatusEnum.INVALID.getNo().equals(status)) {
			return false;
		}
		
		return getActcommitLogService().checkActCommit(null, entity.getMerchantId(), entity.getId(),
				entity.getProcInstId(), "价格方案", "PC");
	}
	
	private ActCommitLogService getActcommitLogService(){
		return (ActCommitLogService)SpringWebContextUtil.getApplicationContext().getBean("actCommitLogService");
	}
	
	private PriceSchemeValidNoticeFacade getPriceSchemeValidNoticeFacade(){
		return (PriceSchemeValidNoticeFacade)SpringWebContextUtil.getApplicationContext().getBean("priceSchemeValidNoticeFacade");
	}
	
	private PriceSchemeServiceFacade getPriceSchemeServiceFacade() {
		return (PriceSchemeServiceFacade)SpringWebContextUtil.getApplicationContext().getBean("priceSchemeServiceFacade");
	}
	
	private ActivitiProcessServiceImpl getActivitiProcessServiceImpl(){
		return (ActivitiProcessServiceImpl)SpringWebContextUtil.getApplicationContext().getBean("activitiProcessServiceImpl");
	}

	
	

}
